package com.example.demo.controller;

import com.example.demo.LoginUser;
import com.example.demo.commom.Result;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.web.bind.annotation.*;
import com.example.demo.utils.TokenUtils;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    public Result<?> register(@RequestBody User user) {
        User existUser = userService.selectByUsername(user.getUsername());
        if (existUser != null) {
            return Result.error("-1", "用户名已重复");
        }
        int result = userService.insert(user);
        return result > 0 ? Result.success() : Result.error("400", "注册失败");
    }

    @CrossOrigin
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) {
        User res = userService.selectByUsernameAndPassword(user.getUsername(), user.getPassword());
        if (res == null) {
            return Result.error("-1", "用户名或密码错误");
        }
        String token = TokenUtils.genToken(res);
        res.setToken(token);
        LoginUser loginUser = new LoginUser();
        loginUser.addVisitCount();
        return Result.success(res);
    }

    @PostMapping
    public Result<?> save(@RequestBody User user) {
        if (user.getPassword() == null) {
            user.setPassword("abc123456");
        }
        int result = userService.insert(user);
        return result > 0 ? Result.success() : Result.error("400", "保存失败");
    }

    @PutMapping("/password")
    public Result<?> updatePassword(@RequestParam Integer id, @RequestParam String password2) {
        int result = userService.updatePassword(id, password2);
        return result > 0 ? Result.success() : Result.error("400", "密码更新失败");
    }

    @PutMapping
    public Result<?> update(@RequestBody User user) {
        int result = userService.updateById(user);
        return result > 0 ? Result.success() : Result.error("400", "更新失败");
    }

    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
        int result = userService.deleteBatchIds(ids);
        return result > 0 ? Result.success() : Result.error("400", "批量删除失败");
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        int result = userService.deleteById(id);
        return result > 0 ? Result.success() : Result.error("400", "删除失败");
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        List<User> users = userService.selectPage(pageNum, pageSize, search);
        int total = userService.selectCount(search);

        Map<String, Object> result = new HashMap<>();
        result.put("records", users);
        result.put("total", total);
        result.put("current", pageNum);
        result.put("size", pageSize);

        return Result.success(result);
    }

    @GetMapping("/usersearch")
    public Result<?> findPage2(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam(defaultValue = "") String search1,
                               @RequestParam(defaultValue = "") String search2,
                               @RequestParam(defaultValue = "") String search3,
                               @RequestParam(defaultValue = "") String search4) {
        List<User> users = userService.selectPageWithMultiSearch(pageNum, pageSize, search1, search2, search3, search4);
        int total = userService.selectCountWithMultiSearch(search1, search2, search3, search4);

        Map<String, Object> result = new HashMap<>();
        result.put("records", users);
        result.put("total", total);
        result.put("current", pageNum);
        result.put("size", pageSize);

        return Result.success(result);
    }
}